package org.example.config;

import org.example.service.model.UserModel;

/**
 * @author xietl
 * @date 2025/10/8  14:48
 * @description 用户上下文模型
 */
public class UserContextConfig {
    private static final ThreadLocal<UserModel> userModelThreadLocal = new ThreadLocal<>();

    /**
     * 设置用户上下文模型
     * @param userModel
     */
    public static void setUserModel(UserModel userModel){
        userModelThreadLocal.set(userModel);
    }

    /**
     * 获取用户上下文模型
     * @return
     */
    public static UserModel getUserModel(){
        return userModelThreadLocal.get();
    }

    /**
     * 获取当前线程的用户ID
     * @return 用户ID
     */
    public static Integer getUserId() {
        UserModel user = userModelThreadLocal.get();
        return user != null ? user.getId() : null;
    }

    /**
     * 获取当前线程的用户名
     * @return 用户名
     */
    public static String getUserName() {
        UserModel user = userModelThreadLocal.get();
        return user != null ? user.getName() : null;
    }

    /**
     * 获取当前线程的用户手机号
     * @return 用户手机号
     */
    public static String getUserPhone() {
        UserModel user = userModelThreadLocal.get();
        return user != null ? user.getTeiphon() : null;
    }

    /**
     * 移除用户上下文模型
     */
    public static void removeUserModel(){
        userModelThreadLocal.remove();
    }
}
